package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.GlueCategory;
import com.ruoyi.system.mapper.GlueCategoryMapper;
import com.ruoyi.system.service.IGlueCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 选胶分类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-31
 */
@Service
public class GlueCategoryServiceImpl extends ServiceImpl<GlueCategoryMapper,GlueCategory> implements IGlueCategoryService
{
    @Autowired
    private GlueCategoryMapper glueCategoryMapper;

    /**
     * 查询选胶分类
     *
     * @param id 选胶分类主键
     * @return 选胶分类
     */
    @Override
    public GlueCategory selectGlueCategoryById(Long id)
    {
        GlueCategory glueCategory = glueCategoryMapper.selectGlueCategoryById(id);
        if (ObjectUtils.isEmpty(glueCategory)){
            return glueCategory;
        }
        return getGlueCategoryRecursion(glueCategory.getId());
    }

    /**
     * 查询选胶分类列表
     *
     * @param glueCategory 选胶分类
     * @return 选胶分类
     */
    @Override
    public List<GlueCategory> selectGlueCategoryList(GlueCategory glueCategory)
    {
        //查询需要的
        List<GlueCategory> glueCategories = glueCategoryMapper.selectGlueCategoryList(glueCategory);

        Set<GlueCategory> glueCategories1 = new HashSet<>();

        for (GlueCategory category : glueCategories) {
            Long upId = getUpId(category);
            glueCategories1.add(getGlueCategoryRecursion(upId));

        }
        ArrayList<GlueCategory> glueCategories2 = new ArrayList<>(glueCategories1);
        //排序
        glueCategories2.sort(Comparator.comparing(GlueCategory::getSortOrder).thenComparing(GlueCategory::getCreateTime,Comparator.reverseOrder()));

        return  glueCategories2;
    }

    /**
     * 递归找上级id
     */
   public Long  getUpId(GlueCategory category){
       if (ObjectUtils.isEmpty(category.getParentId())){
           return category.getId();
       }else {
         return getUpId(this.lambdaQuery().eq(GlueCategory::getId,category.getParentId()).one());
       }
     }




    /**
     * 新增选胶分类
     *
     * @param glueCategory 选胶分类
     * @return 结果
     */
    @Override
    public int insertGlueCategory(GlueCategory glueCategory)
    {
        glueCategory.setCreateTime(DateUtils.getNowDate());
        glueCategory.setCreateBy(SecurityUtils.getUserId().toString());
        return glueCategoryMapper.insertGlueCategory(glueCategory);
    }

    /**
     * 修改选胶分类
     *
     * @param glueCategory 选胶分类
     * @return 结果
     */
    @Override
    public int updateGlueCategory(GlueCategory glueCategory)
    {
        glueCategory.setUpdateTime(DateUtils.getNowDate());
        return glueCategoryMapper.updateGlueCategory(glueCategory);
    }

    /**
     * 批量删除选胶分类
     *
     * @param ids 需要删除的选胶分类主键
     * @return 结果
     */
    @Override
    public int deleteGlueCategoryByIds(Long[] ids)
    {
        return glueCategoryMapper.deleteGlueCategoryByIds(ids);
    }

    /**
     * 删除选胶分类信息
     *
     * @param id 选胶分类主键
     * @return 结果
     */
    @Override
    public int deleteGlueCategoryById(Long id)
    {
        GlueCategory glueCategoryRecursion = getGlueCategoryRecursion(id);
        if (!ObjectUtils.isEmpty(glueCategoryRecursion)&&!CollectionUtils.isEmpty(glueCategoryRecursion.getSonGlueCategory())){
            throw  new RuntimeException("该分类有下级无法删除，请先删除下级");
        }

        return glueCategoryMapper.deleteGlueCategoryById(id);
    }

    /**
     * 递归查询下分类
     */
    public GlueCategory getGlueCategoryRecursion(Long id){
        GlueCategory glueCategory = this.lambdaQuery().eq(GlueCategory::getId, id).one();
        if (ObjectUtils.isEmpty(glueCategory)){
            return null;
        }
        recursion(glueCategory);
        return glueCategory;
    }

    /**
     * 递归填充下级
     */
    public void recursion( GlueCategory glueCategory){
        List<GlueCategory> list = this.lambdaQuery().eq(GlueCategory::getParentId, glueCategory.getId()).list();
        glueCategory.setSonGlueCategory(list);
        if (!CollectionUtils.isEmpty(list)){
            for (GlueCategory category : list) {
                recursion(category);
            }
        }
    }


    /**
     * 根据父id查询所有子id
     */
    @Override
    public List<Long> getIds(Long id) {
        List<Long> longs = new ArrayList<>();
        if (ObjectUtils.isEmpty(id)){
            return longs;
        }
        longs.add(id);
        getIdsRecursion(id,longs);
        return longs;
    }

    /**
     * 递归填充子id
     */
    public void  getIdsRecursion(Long id, List<Long> longs){
        List<GlueCategory> list = this.lambdaQuery().eq(GlueCategory::getParentId, id).list();
        if (!CollectionUtils.isEmpty(list)){
            longs.addAll(list.stream().map(GlueCategory::getId).collect(Collectors.toSet()));
            for (GlueCategory glueCategory : list) {
                getIdsRecursion(glueCategory.getId(),longs);
            }
        }
    }
}
